var id=0;
function createDiv(parent,childId) {
        //alert(parent+" "+childId);
        alert(parent.id);
        var divTag = document.createElement("div");
        divTag.id = childId;
        divTag.setAttribute("align", "center");
        divTag.style.margin = "0px auto";
        divTag.className = "dynamicDiv";
        divTag.innerHTML = "This HTML Div tag created "
     //   document.body.appendChild(divTag);
        parent.appendChild(divTag);
  }
/* center box */
var has_inner = typeof(window.innerWidth) == 'number';
var has_element = document.documentElement
    && document.documentElement.clientWidth;

function center_box(box, width, height)
{
    cleft = has_inner
        ? pageXOffset + 
          (window.innerWidth - width)/2
        : has_element
          ? document.documentElement.scrollLeft + 
            (document.documentElement.clientWidth - width)/2
          : document.body.scrollLeft + 
            (document.body.clientWidth - width)/2;
    ctop = has_inner
        ? pageYOffset + (window.innerHeight - height)/2
        : has_element
          ? document.documentElement.scrollTop + 
            (document.documentElement.clientHeight - height)/2
          : document.body.scrollTop + 
            (document.body.clientHeight - height)/2;

    box.style.left = cleft > 0 ? cleft + 'px' : '0px';
    box.style.top = ctop > 0 ? ctop + 'px' : '0px';
}
  

function createInput(parent,childId,type) {
        //alert(parent+" "+childId);
//      alert(parent+" AAA "+parent.innerHTML);
//	alert(parent.id,slidecon);
        var dialogDiv = document.createElement("div");
    dialogDiv.id="dialogBox";
	dialogDiv.setAttribute("align", "center");
	dialogDiv.style.margin = "auto";
	dialogDiv.style.position = "fixed";
	dialogDiv.style.height="800px";	
	dialogDiv.style.width="1500px";	
	dialogDiv.style.backgroundColor="#efefef";
	dialogDiv.style.left="0%";
	dialogDiv.style.top ="0px";
//	 document.body.appendChild(dialogDiv);
	var element = document.createElement("textarea");

        element.id = childId;
	element.setAttribute("align", "center");
        element.style.margin = "auto";
        element.style.position = "fixed";
        element.style.top ="60px";
        element.style.left="20%";
//	center_box(element,800,400);  	
      element.style.height="400px";
       element.style.width= "800px";
        element.style.backgroundColor="#efefef";
        element.style.border="2px solid #0000ff";
        //element.className = "dialogBox";
        element.value = slideContent[currentSlide];// parent.innerHTML; 
       // element.innerHTML = parent.innerHTML; 
       // element.innerHTML = parent.innerHTML;
//	document.createElement("a");
	dialogDiv.innerHTML="<a href=# onclick='saveSlide();'>Save Slide</a>"; 
dialogDiv.innerHTML+="<a href=# onclick='document.body.removeChild(document.getElementById(\"dialogBox\"));'> x</a>";

  // dialogDiv.appendChild(element);
	 document.body.appendChild(dialogDiv);
	dialogDiv.appendChild(element);
  }
  
function deleteDiv(divId) {
//            var divTag = document.deleteElement("div");
//var divname="div1"
document.body.removeChild(document.getElementById('div'+id));
//alert(id)
}
function saveSlide(){
var content=document.getElementById("editField").value;
//alert(currentSlide+" "+ presId+" " +content)
 ajax = new AJAX();
          var dosya="ajax/saveSlide.php"; 
	//?slideId='+currentSlide+'&presId='+presId+'&content='+content; 
	
	//var KULLANICI_ADI = trim(document.getElementById('kullaniciAdi').value);
	

	
          if ( ajax ) {
              ajax.onreadystatechange = function () {};
              ajax.abort();
          }

          ajax.onreadystatechange = function () {

            if( ajax.readyState == 4 ) {

                  function AJAX() {};
              //   alert(ajax.responseText);


        //     alert(ajax.responseText);
       	 document.getElementById('statBar').innerHTML='slide saved';
                }

          };
	//alert(presId+" " + currentSlide + " " +content);
	var data = "presId="+presId+"&slideId="+currentSlide+"&content="+content;
        ajax.open('POST', dosya , true);
        ajax.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
     	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	ajax.setRequestHeader("Connection", "close");
        ajax.send(data);
	document.body.removeChild(document.getElementById('dialogBox'));
}

function AJAX() {
         var ajax = false;
         // Internet Explorer (5.0+)
         try {
           ajax = new ActiveXObject("Msxml2.XMLHTTP");  // yeni versiyon xmlhttp
         } catch (e) {
            try {
              ajax = new ActiveXObject("Microsoft.XMLHTTP");  // eski versiyon
																// xmlhttp
            } catch (e) {
              ajax = false;
            }
         }
         // Mozilla ve Safari
         if ( !ajax && typeof XMLHttpRequest != 'undefined' ) {
           try{
              ajax = new XMLHttpRequest();
           }catch(e) {   
              ajax = false;
           }
         }
         // Diger
         if ( !ajax && window.createRequest ) {
           try{
              ajax = window.createRequest();
           }catch(e) { 
              ajax = false;
           }
         }
          return ajax;
      }


function getSlideInfo(slideId) {
		//alert(presId);
          ajax = new AJAX();
         
         
          var dosya= 'ajax/getSlideInfo.php?slideId='+slideId+'&presId='+presId;

          if ( ajax ) {
              ajax.onreadystatechange = function () {};
              ajax.abort();
          }
          ajax.open('GET', dosya , true);
          ajax.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
          ajax.setRequestHeader("Connection", "close");
         
          ajax.send(null);
         
          ajax.onreadystatechange = function () {

            if( ajax.readyState == 4 ) {
		           	
		  function AJAX() {};
		//  alert(ajax.responseText);
		var aaa='';
		aaa=ajax.responseText; 
	document.getElementById('slide_overview').innerHTML=aaa;
	//              	return 	 aaa;
		}
            
          };
      }


function getContent(id){
var cont;
cont = getSlideInfo(id);
//alert(cont);
return "Content of slide"+cont;

}


function updateSlideOverview(id){
//alert(id);
var content;
currentSlide=id;
getSlideInfo(id);
//document.getElementById('slide_overview').innerHTML=content;

}


function savePresentation(){

//presId=1;
//alert(id);
   ajax = new AJAX();
         
         
          var dosya= 'ajax/savePresentation.php?presId='+presId+'&slideNum='+id;

          if ( ajax ) {
              ajax.onreadystatechange = function () {};
              ajax.abort();
          }
          ajax.open('GET', dosya , true);
          ajax.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
          ajax.setRequestHeader("Connection", "close");
         
          ajax.send(null);
         
          ajax.onreadystatechange = function () {
            if( ajax.readyState == 4 ) {
		  function AJAX() {};
		//  alert(ajax.responseText);
		document.getElementById('statBar').innerHTML='saved:'+Date();
		}
          };
      }


/* ########### HTML ELEMENTS ############### */

function createSlideThumbnail() {
        var divTag = document.createElement("div");
	id++;

           divTag.id = "div"+id;
           divTag.setAttribute("align", "center");
           divTag.setAttribute("onClick", "updateSlideOverview("+id+");");
           divTag.style.margin = "0px auto";
           divTag.className = "dynamicDiv";
           divTag.innerHTML = slideContent[id];
	   document.getElementById('slides_overview').innerHTML+=id+"<br>";
    //       document.body.appendChild(divTag);
	document.getElementById('slides_overview').appendChild(divTag);
	document.getElementById('li-'+id+'-1').style.fontSize="5px";
	document.getElementById('li-'+id+'-2').style.fontSize="5px";
	document.getElementById('li-'+id+'-3').style.fontSize="5px";
	document.getElementById('li-'+id+'-4').style.fontSize="5px";
	document.getElementById('li-'+id+'-5').style.fontSize="5px";
//alert(id)
//id++;
  }
function deleteSlideThumbnail() {
	if(id>0){
		//document.body.removeChild(document.getElementById('div'+id));
		document.getElementById('slides_overview').removeChild(document.getElementById('div'+id));
		//alert(id)
		id--;
		}

}





function removeElement(parentDiv, childDiv){
    if (childDiv == parentDiv) {
         alert("The parent div cannot be removed.");
    }
    else if (document.getElementById(childDiv)) {
         var child = document.getElementById(childDiv);
         var parent = document.getElementById(parentDiv);
         parent.removeChild(child);
    }
    else {
         alert("Child div has already been removed or does not exist.");
         return false;
    }
}
